package com.jinwei.qunguanjia.web.system.controller;

import com.jinwei.qunguanjia.common.annotation.Log;
import com.jinwei.qunguanjia.common.constant.UserConstants;
import com.jinwei.qunguanjia.common.core.domain.Response;
import com.jinwei.qunguanjia.common.core.page.PageDataInfo;
import com.jinwei.qunguanjia.common.enums.BusinessTypeEnum;
import com.jinwei.qunguanjia.common.utils.JsonUtils;
import com.jinwei.qunguanjia.common.utils.PageDataUtil;
import com.jinwei.qunguanjia.common.utils.PageUtils;
import com.jinwei.qunguanjia.common.utils.poi.ExcelUtil;
import com.jinwei.qunguanjia.system.dto.SysDictTypeDTO;
import com.jinwei.qunguanjia.system.service.SysDictTypeService;
import com.jinwei.qunguanjia.web.system.vo.SysDictTypeVO;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 数据字典信息
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/dict/type")
public class SysDictTypeController {

    @Resource
    private SysDictTypeService sysDictTypeService;

    /**
     * 新增字典类型
     * @param sysDictTypeVO 对象
     */
    @PreAuthorize("@ss.hasPermission('system:dict:save')")
    @Log(title = "字典类型", businessType = BusinessTypeEnum.INSERT)
    @PostMapping("/save")
    public Response save(@Validated @RequestBody SysDictTypeVO sysDictTypeVO) throws IllegalAccessException {
        SysDictTypeDTO sysDictTypeDTO = new SysDictTypeDTO();
        BeanUtils.copyProperties(sysDictTypeVO, sysDictTypeDTO);
        if (UserConstants.NOT_UNIQUE.equals(sysDictTypeService.checkDictTypeUnique(sysDictTypeDTO))) {
            return Response.fail("新增字典'" + sysDictTypeVO.getDictName() + "'失败，字典类型已存在");
        }
        sysDictTypeService.save(sysDictTypeDTO);
        return Response.success();
    }

    /**
     * 修改字典类型
     * @param sysDictTypeVO 对象
     */
    @PreAuthorize("@ss.hasPermission('system:dict:update')")
    @Log(title = "字典类型", businessType = BusinessTypeEnum.UPDATE)
    @PutMapping("/update")
    public Response update(@Validated @RequestBody SysDictTypeVO sysDictTypeVO) throws IllegalAccessException {
        SysDictTypeDTO sysDictTypeDTO = new SysDictTypeDTO();
        BeanUtils.copyProperties(sysDictTypeVO, sysDictTypeDTO);
        if (UserConstants.NOT_UNIQUE.equals(sysDictTypeService.checkDictTypeUnique(sysDictTypeDTO))) {
            return Response.fail("修改字典'" + sysDictTypeVO.getDictName() + "'失败，字典类型已存在");
        }
        sysDictTypeService.update(sysDictTypeDTO);
        return Response.success();
    }

    /**
     * 删除字典类型
     * @param ids id列表
     */
    @PreAuthorize("@ss.hasPermission('system:dict:remove')")
    @Log(title = "字典类型", businessType = BusinessTypeEnum.DELETE)
    @DeleteMapping("/{ids}")
    public Response remove(@PathVariable List<String> ids) {
        sysDictTypeService.remove(ids);
        return Response.success();
    }

    @PreAuthorize("@ss.hasPermission('system:dict:list')")
    @GetMapping("/list")
    public PageDataInfo<SysDictTypeVO> list(SysDictTypeVO sysDictTypeVO) throws IllegalAccessException {
        SysDictTypeDTO sysDictTypeDTO = new SysDictTypeDTO();
        BeanUtils.copyProperties(sysDictTypeVO, sysDictTypeDTO);
        PageUtils.startPage();
        PageDataInfo<SysDictTypeDTO> pageDataInfo = sysDictTypeService.listPage(sysDictTypeDTO);
        PageDataUtil<SysDictTypeDTO, SysDictTypeVO> pageDataUtil = new PageDataUtil<>();
        return pageDataUtil.transform(pageDataInfo, SysDictTypeVO.class);
    }

    @Log(title = "字典类型", businessType = BusinessTypeEnum.EXPORT)
    @PreAuthorize("@ss.hasPermission('system:dict:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysDictTypeVO sysDictTypeVO) throws IllegalAccessException {
        SysDictTypeDTO sysDictTypeDTO = new SysDictTypeDTO();
        BeanUtils.copyProperties(sysDictTypeVO, sysDictTypeDTO);
        List<SysDictTypeDTO> list = sysDictTypeService.list(sysDictTypeDTO);
        ExcelUtil<SysDictTypeVO> util = new ExcelUtil<>(SysDictTypeVO.class);
        util.exportExcel(response, JsonUtils.copyList(list, SysDictTypeVO.class), "字典类型");
    }

    /**
     * 查询字典类型详细
     */
    @PreAuthorize("@ss.hasPermission('system:dict:query')")
    @GetMapping(value = "/{dictId}")
    public Response<SysDictTypeVO> getInfo(@PathVariable String dictId) {
        SysDictTypeVO sysDictTypeVO = new SysDictTypeVO();
        BeanUtils.copyProperties(sysDictTypeService.get(dictId), sysDictTypeVO);
        return Response.success(sysDictTypeVO);
    }

    /**
     * 刷新字典缓存
     */
    @PreAuthorize("@ss.hasPermission('system:dict:remove')")
    @Log(title = "字典类型", businessType = BusinessTypeEnum.REFRESH_CACHE)
    @DeleteMapping("/refreshCache")
    public Response refreshCache() throws IllegalAccessException {
        sysDictTypeService.resetDictCache();
        return Response.success();
    }

    /**
     * 获取字典选择框列表
     */
    @GetMapping("/optionSelect")
    public Response<List<SysDictTypeVO>> optionSelect() throws IllegalAccessException {
        return Response.success(JsonUtils.copyList(sysDictTypeService.list(SysDictTypeDTO.builder().build()), SysDictTypeVO.class));
    }
}
